<script name="CallbackBind" lang="ts" setup>
// 获取qq用户信息
import { ref } from 'vue'
import request from '@/utils/request'
import useStore from '@/store'
import { HmMessage } from '@/components/message/message'
import { useRouter } from 'vue-router'
const props = defineProps({
  openIdStr: {
    type: String,
    required: true
  }
})
const avatar = ref('')
const username = ref('')
if (QC.Login.check()) {
  // 获取用户信息
  QC.api('get_user_info').success((res: any) => {
    console.log('get_user_info用户信息', res)
    avatar.value = res.data.figureurl_qq
    username.value = res.data.nickname
  })
}
const mobile = ref('13666666666')
const totalTime = ref(6)
let timeInterval = 0
const getCode = async () => {
  // 倒计时：表单验证通过，且接口调用前
  totalTime.value--
  timeInterval = window.setInterval(() => {
    totalTime.value--
    if (totalTime.value <= 0) {
      window.clearInterval(timeInterval)
      totalTime.value = 6
    }
  }, 1000)

  await request.request({
    url: '/login/social/code',
    params: {
      mobile: mobile.value
    }
  })
}

// 绑定处理
const { user } = useStore()
const router = useRouter()
const submit = async () => {
  // 表单验证
  await user.loginSocialBind(props.openIdStr, mobile.value)
  //   提示一下
  HmMessage.success?.('绑定成功')
  // 跳转到首页
  router.push('/')
}
</script>
<template>
  <div class="xtx-form">
    <div class="user-info">
      <img :src="avatar" alt="" />
      <p>Hi，{{ username }} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input
          v-model="mobile"
          class="input"
          type="text"
          placeholder="绑定的手机号"
        />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input class="input" type="text" placeholder="短信验证码" />
        <span class="code" @click="getCode" v-if="totalTime === 6"
          >发送验证码</span
        >
        <span class="code" v-else>倒计时:{{ totalTime }}s</span>
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="submit">立即绑定</a>
  </div>
</template>

<style scoped lang="less">
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
